package com.ebelter.nb.ui.fragments;


import android.content.Intent;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;

import com.ebelter.btcomlib.models.bluetooth.products.bpm.BpmManager;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.LevelColorView2;
import com.ebelter.nb.R;
import com.ebelter.nb.beans.GetBP;
import com.ebelter.nb.model.http.request.NbNetUtils;
import com.ebelter.nb.model.jg.jgbean.JGBpmR;
import com.ebelter.nb.ui.activitys.MainActivity;
import com.ebelter.nb.ui.activitys.TrendActivity;
import com.ebelter.nb.ui.view.CombinView5;
import com.ebelter.nb.utils.NbUtitls;

import org.greenrobot.eventbus.EventBus;


/**
 * 血压计
 */

public class BPM_Fragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "BPM_Fragment";

    private String titleStr="血压计";

    LevelColorView2 bpm_lcv;
    CombinView5 cb5_bpm1;
    CombinView5 cb5_bpm2;
    CombinView5 cb5_bpm3;
    LinearLayout bpm_trend_ll;

    private TextView activityTopTittleTv;
    private TextView bpm_result_tip_tv;
    private LinearLayout bpm_result_tip_ll;

    private ImageView bpm_ic_iv;
    private TextView bpm_tip_tv;
    private TextView bpm_mesure_time_tv;

    private int[] userLevels = new int[]{-1,-1};
    private String[] user0 = new String[]{"0","0","0"};
    private String[] user1 = new String[]{"0","0","0"};
    private String[] times = new String[]{"",""};
    private String[] userMesureResultTip = new String[]{"",""};
    private String[] userMesureResultTip2 = new String[]{"血压计","血压计"};


    public void setActivityTopTittleTv(TextView activityTopTittleTv) {
        this.activityTopTittleTv = activityTopTittleTv;
    }
    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_bpm;
    }
    @Override
    protected void initView() {
        super.initView();
        FV();
        setListener();
    }

    public String getTitleStr(){
        return titleStr;
    }

    private void updateSexIcon(){
        int sex = NbUtitls.getSex();
        ViewUtils.setImageResource(bpm_ic_iv,sex==0?R.drawable.ic_man:R.drawable.ic_women);
    }
    @Override
    protected void setListener() {
        bpm_trend_ll.setOnClickListener(this);
    }

    // int level;
    private void FV() {
        bpm_lcv=mRootView.findViewById(R.id.bpm_lcv);
        bpm_trend_ll = mRootView.findViewById(R.id.bpm_trend_ll);

        cb5_bpm1 = mRootView.findViewById(R.id.cb5_shzhy);
        cb5_bpm2 = mRootView.findViewById(R.id.cb5_shsy);
        cb5_bpm3=mRootView.findViewById(R.id.cb5_mb);

        bpm_result_tip_tv=mRootView.findViewById(R.id.bpm_result_tip_tv);
        bpm_result_tip_ll=mRootView.findViewById(R.id.bpm_result_tip_ll);

        bpm_ic_iv=mRootView.findViewById(R.id.bpm_ic_iv);
        bpm_tip_tv=mRootView.findViewById(R.id.bpm_tip_tv);

        bpm_mesure_time_tv = mRootView.findViewById(R.id.bpm_mesure_time_tv);



    }


    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        bpm_lcv.setLevel(-1);
        initSuggestStrRes();

        getLastBpmMesure();
    }

    private void getLastBpmMesure() {
        if (!NetUtils.available()){
            return;
        }

        String authKey = NbUtitls.getLoginAuthkey();
        NbNetUtils.getInstance().getBloodPressurePage(this, authKey, 1, 1,"1", new HttpResponse<GetBP>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, GetBP getBP, String s) {

                LogUtils.i(TAG,"-getLastBpmMesure--s = "+s);
                LogUtils.i(TAG,"--getLastBpmMesure--getBP = " + getBP);
                if (isRequestSuccess&&getBP.resultData!=null){
                    if (getBP.resultData.dataList.size()>0){

                        GetBP.ResultDataBean.DataListBean bean = getBP.resultData.dataList.get(0);

                        JGBpmR jgBpmR =new JGBpmR();
                        JGBpmR.DeviceInfoBean d=new JGBpmR.DeviceInfoBean();
                        d.sys=bean.systolic+"";
                        d.dia=bean.diastolic+"";
                        d.user="1";
                        d.pul=bean.pulse+"";
                        d.time=bean.time;
                        jgBpmR.deviceInfo=d;
                        setBpmData(jgBpmR);
                        disUserData(1);

                    }

                }

            }
        });

    }


    @Override
    public void onClick(View v) {
        int vid=v.getId();
        if (vid==R.id.bpm_trend_ll){
            Intent intent=new Intent(getContext(), TrendActivity.class);
            intent.putExtra(TrendActivity.KEY,TrendActivity.Trend_XUEYA);
            intent.putExtra(TrendActivity.USER,userIndex==1? TrendActivity.Trend_USER_1:TrendActivity.Trend_USER_2);
            startActivity(intent);
        }
    }


    //设置结果值
    public void setBpmData(JGBpmR jGBpmR) {
        if (jGBpmR!=null&&jGBpmR.deviceInfo!=null){

            int sys=NumUtils.string2Int(jGBpmR.deviceInfo.sys);
            int dia=NumUtils.string2Int(jGBpmR.deviceInfo.dia);
            setTip(sys,dia);
            if (TextUtils.equals("1",jGBpmR.deviceInfo.user)){
                //用户0
                user0[0] = jGBpmR.deviceInfo.sys;
                user0[1] = jGBpmR.deviceInfo.dia;
                user0[2] = jGBpmR.deviceInfo.pul;
                userLevels[0] = BpmManager.getMesureResultLevel(sys,dia);
                userMesureResultTip[0] =getXYSuggestion(sys,dia);
                userMesureResultTip2[0] =BpmManager.getMesureResultDesc(sys,dia);
                times[0]=jGBpmR.deviceInfo.time;
            }else if (TextUtils.equals("2",jGBpmR.deviceInfo.user)){
                //用户1
                user1[0] = jGBpmR.deviceInfo.sys;
                user1[1] = jGBpmR.deviceInfo.dia;
                user1[2] = jGBpmR.deviceInfo.pul;
                userLevels[1] = BpmManager.getMesureResultLevel(sys,dia);
                userMesureResultTip[1] =getXYSuggestion(sys,dia);
                userMesureResultTip2[1] =BpmManager.getMesureResultDesc(sys,dia);
                times[1]=jGBpmR.deviceInfo.time;
            }
            updateSexIcon();
        }
    }

    private void setTip(int ss,int sz){
        String r = BpmManager.getMesureResultDesc(ss,sz);
        ViewUtils.setTextViewStr(bpm_tip_tv,"测试结果:"+r);
    }

    private int userIndex = 1;
    //显示结果值
    public void disUserData(int index){//index 1或者2

        if (index==1){
            userIndex=1;
            NbUtitls.setCb5Value(cb5_bpm1,2,user0[0]);
            NbUtitls.setCb5Value(cb5_bpm2,2,user0[1]);
            NbUtitls.setCb5Value(cb5_bpm3,2,user0[2]);
            if (TextUtils.isEmpty(userMesureResultTip[0])){
                ViewUtils.hideView(bpm_result_tip_ll);
            }else {
                ViewUtils.displayView(bpm_result_tip_ll);
                ViewUtils.setTextViewStr(bpm_result_tip_tv,userMesureResultTip[0]);
            }

            if (bpm_lcv!=null)bpm_lcv.setLevel(userLevels[0]);
            int sys=NumUtils.string2Int(user0[0]);
            int dia=NumUtils.string2Int(user0[1]);
            setTip(sys,dia);
            ViewUtils.setTextViewStr(bpm_mesure_time_tv,"测量时间:"+times[0]);
            titleStr=userMesureResultTip2[0];
        }else {
            userIndex=2;
            NbUtitls.setCb5Value(cb5_bpm1,2, user1[0]);
            NbUtitls.setCb5Value(cb5_bpm2,2, user1[1]);
            NbUtitls.setCb5Value(cb5_bpm3,2, user1[2]);

            if (TextUtils.isEmpty(userMesureResultTip[1])){
                ViewUtils.hideView(bpm_result_tip_ll);
            }else {
                ViewUtils.displayView(bpm_result_tip_ll);
                ViewUtils.setTextViewStr(bpm_result_tip_tv,userMesureResultTip[1]);
            }
            if (bpm_lcv!=null)bpm_lcv.setLevel(userLevels[1]);
            int sys=NumUtils.string2Int(user1[0]);
            int dia=NumUtils.string2Int(user1[1]);
            setTip(sys,dia);
            ViewUtils.setTextViewStr(bpm_mesure_time_tv,"测量时间:"+times[1]);
            titleStr=userMesureResultTip2[1];
        }

        CommonEventBus commonEventBus = new CommonEventBus(TAG, MainActivity.TAG,88,null);
        EventBus.getDefault().post(commonEventBus);

    }


    private String dixueya3;
    private String lixiangxueya;
    private String zhengchangxueya;
    private String zhengchangaoxueyagaozhi;
    private String linjiegaoxueya25;
    private String danchunshousuoxinggaoxueya;
    private String yijigaoxueya;
    private String erjigaoxueya;
    private String sanjigaoxueya;

    private void initSuggestStrRes(){
        dixueya3=getString(R.string.dixueya3);
        lixiangxueya=getString(R.string.lixiangxueya);
        zhengchangxueya=getString(R.string.zhengchangxueya);
        zhengchangaoxueyagaozhi=getString(R.string.zhengchangaoxueyagaozhi);
        linjiegaoxueya25=getString(R.string.linjiegaoxueya25);
        danchunshousuoxinggaoxueya=getString(R.string.danchunshousuoxinggaoxueya);
        yijigaoxueya=getString(R.string.yijigaoxueya);
        erjigaoxueya=getString(R.string.erjigaoxueya);
        sanjigaoxueya=getString(R.string.sanjigaoxueya);
    }

    //获取血压建议
    private String getXYSuggestion(int sys,int dia){
        if (sys<=90||dia<=60){
            return dixueya3;
        }else if ((sys>90&&sys<120)&&(dia>60&&dia<80)){
            return lixiangxueya;
        }else if ((sys>=90&&sys<130)||(dia>60&&dia<85)){
            return zhengchangxueya;
        }else if ((sys>=130&&sys<=139)||(dia>=85&&dia<=89)){
            return zhengchangaoxueyagaozhi;
        }else if ((sys>=140&&sys<=149)||(dia>=90&&dia<=94)){
            return linjiegaoxueya25;
        }else if ((sys>=140)&&(dia<90)){
            return danchunshousuoxinggaoxueya;
        }else if ((sys>=140&&sys<=159)||(dia>=90&&dia<=99)){
            return yijigaoxueya;
        }else if ((sys>=160&&sys<=179)||(dia>=100&&dia<=109)){
            return erjigaoxueya;
        }else if ((sys>=180)||(dia>=110)){
            return sanjigaoxueya;
        }
        return null;
    }
}
